package com.javayh.oauth.base;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义认证用户信息
 *
 */
public class BaseUserDetails implements UserDetails {

	private static final long serialVersionUID = -123308657146774881L;

	/**
	 * 用户ID
	 */
	private Long userId;

	/**
	 * 登录名
	 */
	private String username;

	/**
	 * 密码
	 */
	private String password;

	/**
	 * 用户权限
	 */
	private Collection<? extends GrantedAuthority> authorities;

	/**
	 * 是否已锁定
	 */
	private boolean accountNonLocked;

	/**
	 * 是否已过期
	 */
	private boolean accountNonExpired;

	/**
	 * 是否启用
	 */
	private boolean enabled;

	/**
	 * 密码是否已过期
	 */
	private boolean credentialsNonExpired;

	/**
	 * 认证客户端ID
	 */
	private String clientId;

	/**
	 * 认证中心域,适用于区分多用户源,多认证中心域
	 */
	private String domain;

	/**
	 * 昵称
	 */
	private String nickName;

	/**
	 * 头像
	 */
	private String avatar;

	/**
	 * 账户Id
	 */
	private Long accountId;

	/***
	 * 账户类型
	 */
	private String accountType;

	/**
	 * 用户附加属性
	 */
	private Map<String, Object> attrs;

	/**
	 * 只是客户端模式.不包含用户信息
	 * @return
	 */
	public Boolean isClientOnly() {
		return clientId != null && username == null;
	}

	/**
	 * 邮箱
	 * @return
	 */
	private String email;

	/**
	 * 是否是管理员
	 * @return
	 */
	private Boolean isAdmin;

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if (authorities == null) {
			return Collections.EMPTY_LIST;
		}
		return this.authorities;
	}

	@JsonIgnore
	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public String getUsername() {
		return this.username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return this.accountNonExpired;
	}

	@Override
	public boolean isAccountNonLocked() {
		return this.accountNonLocked;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return this.credentialsNonExpired;
	}

	@Override
	public boolean isEnabled() {
		return this.enabled;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAccountNonLocked(boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	public void setAccountNonExpired(boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setCredentialsNonExpired(boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public Long getAccountId() {
		return accountId;
	}

	public void setAccountId(Long accountId) {
		this.accountId = accountId;
	}

	public String getAccountType() {
		return accountType;
	}

	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}

	public Map<String, Object> getAttrs() {
		if (attrs == null) {
			return new HashMap();
		}
		return attrs;
	}

	public void setAttrs(Map<String, Object> attrs) {
		this.attrs = attrs;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Boolean getAdmin() {
		return isAdmin;
	}

	public void setAdmin(Boolean admin) {
		isAdmin = admin;
	}

}
